import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;

/**
 * 用于管理骨牌相关操作的类
 */
public class DominoManager {
    /**
     * 骨牌管理者实例
     */
    public static DominoManager Instance = new DominoManager();
    String[] DominoName = {"00","01","02","03","04","05","06","11","12","13","14","15","16","22","23",
            "24","25","26","33","34","35","36","44","45","46","55","56","66"};//全部骨牌共28张的名字
    int[] LibraryIndex = new int[28];//打乱的1~28代表牌序
    int NowIndex = 0;//当前拿牌的下标
    List<Dominoes> Library = new ArrayList<>();//所有骨牌的集合

    /**
     * 初始化
      */
    public void Init(){
        for(String name: DominoName){
            Library.add(new Dominoes(name));
        }
        Shuffle();
    }

    /**
     * 向服务端发出洗牌指令，以获得统一的牌序
     */
    private void Shuffle(){
        MainFrame.MyWriter.send("Shuffle");
    }

    /**
     *获取一张骨牌
     * @return 一张骨牌实例
     */
    public Dominoes GetADomino(){
        return Library.get(LibraryIndex[NowIndex++]);
    }

    /**
     * 以服务端put指令传来的骨牌名字将骨牌实例化
     * @param name 骨牌名字
     * @return 一个骨牌实例
     */
    public Dominoes DominoInstantiate(String name){
        int pos = 0;
        for(String s:DominoName){
            if(name.equals(s)){
                break;
            }
            pos++;
        }
       return DominoManager.Instance.Library.get(pos);
    }

    /**
     * 设置牌序
     * @param tmp 牌序数组
     */
    public void setLibraryIndex(int[] tmp){
        LibraryIndex = tmp;
    }

    /**
     * 用于设定下张牌的角标，对方拿的牌己方跳过
     * @param num 下一张牌的角标
     */
    public void setNowIndex(int num){
        NowIndex = num;
    }
}
